import requests
import os
import json

# 获取当前路径
current_path = os.path.dirname(__file__)


# 读取cooike文件
def get_cookies():
    users = list()
    cookies = list()
    pushkeys = list()
    with open(file=current_path + r'/users.json', mode='r',
              encoding='utf-8') as f:
        json_data = json.load(f)
        for cookie in json_data:
            users.append(json_data[cookie]['user'])
            cookies.append(json_data[cookie]['cookie'])
            pushkeys.append(json_data[cookie]['pushkey'])
    return users, cookies, pushkeys


def login(user, cookie, pushkey):
    try:
        url = "https://glados.rocks/api/user/checkin"
        url2 = "https://glados.rocks/api/user/status"
        referer = 'https://glados.rocks/console/checkin'
        origin = "https://glados.rocks"
        useragent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
        payload = {'token': 'glados.network'}
        checkin = requests.post(url,
                                headers={
                                    'cookie':
                                    cookie,
                                    'referer':
                                    referer,
                                    'origin':
                                    origin,
                                    'user-agent':
                                    useragent,
                                    'content-type':
                                    'application/json;charset=UTF-8'
                                },
                                data=json.dumps(payload))
        state = requests.get(url2,
                             headers={
                                 'cookie': cookie,
                                 'referer': referer,
                                 'origin': origin,
                                 'user-agent': useragent
                             })
        # print(state.text)
        # 输出提示
        if 'message' in checkin.text:
            mess = checkin.json()['message']
            user = state.json()['data']['email']
            time = state.json()['data']['leftDays']
            time = time.split('.')[0]
            requests.get(
                url='https://api2.pushdeer.com/message/push?pushkey=' +
                pushkey + '&text=用户：' + user + r'    信息：' + mess +
                r'    剩余天数：' + time)
            print("用户:" + user)
            print("信息：" + mess)
            print("剩余天数:" + time)
        else:
            requests.get(
                url='https://api2.pushdeer.com/message/push?pushkey=' +
                pushkey + '&text=cookie过期')
            print("cookie过期")
    except Exception:
        print("用户：" + user + "\n信息：cookie错误")
        requests.get(url='https://api2.pushdeer.com/message/push?pushkey=' +
                     pushkey + '&text=cookie错误')


if __name__ == '__main__':
    users, cookies, pushkeys = get_cookies()
    for user, cookie, pushkey in zip(users, cookies, pushkeys):
        login(user=user, cookie=cookie, pushkey=pushkey)
